using BlazorCRUD.App.Models;
using Microsoft.AspNetCore.Mvc;

namespace BlazorCRUD.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase
{
    private static List<ProductModel> InitDatas = new List<ProductModel>()
    {
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 250",
            Description = "QJMOTOR 250",
            Quantity = 1,
            Price = 17999,
            CreatedDate = DateTime.Now.AddDays(-50),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 300",
            Description = "QJMOTOR 300",
            Quantity = 1,
            Price = 23999,
            CreatedDate = DateTime.Now.AddDays(-48),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 350",
            Description = "QJMOTOR 350",
            Quantity = 1,
            Price = 26999,
            CreatedDate = DateTime.Now.AddDays(-46),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 400",
            Description = "QJMOTOR 400",
            Quantity = 1,
            Price = 28999,
            CreatedDate = DateTime.Now.AddDays(-44),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 450",
            Description = "QJMOTOR 450",
            Quantity = 1,
            Price = 29999,
            CreatedDate = DateTime.Now.AddDays(-42),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 500",
            Description = "QJMOTOR 500",
            Quantity = 1,
            Price = 30999,
            CreatedDate = DateTime.Now.AddDays(-40),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 600",
            Description = "QJMOTOR 600",
            Quantity = 1,
            Price = 32999,
            CreatedDate = DateTime.Now.AddDays(-38),
        },
        new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = "QJMOTOR 700",
            Description = "QJMOTOR 700",
            Quantity = 1,
            Price = 34999,
            CreatedDate = DateTime.Now.AddDays(-36),
        },
    };

    [HttpPost]
    public Task<ProductModel> CreateAsync([FromBody] CreateProductModel model)
    {
        var productModel = new ProductModel()
        {
            Id = Guid.NewGuid(),
            Name = model.Name,
            Description = model.Description,
            Quantity = model.Quantity,
            Price = model.Price,
            CreatedDate = DateTime.Now,
        };
        InitDatas.Add(productModel);
        return Task.FromResult(productModel);
    }

    [HttpPut("{id}")]
    public Task<ProductModel> UpdateAsync(Guid id, [FromBody] CreateProductModel model)
    {
        var productModel = InitDatas.First(i => i.Id == id);
        productModel.Name = model.Name;
        productModel.Description = model.Description;
        productModel.Quantity = model.Quantity;
        productModel.Price = model.Price;
        return Task.FromResult(productModel);
    }

    [HttpDelete("{id}")]
    public Task DeleteAsync(Guid id)
    {
        InitDatas.RemoveAll(i => i.Id == id);
        return Task.CompletedTask;
    }

    [HttpGet]
    public Task<List<ProductModel>> GetAsync()
    {
        return Task.FromResult(InitDatas);
    }

    [HttpGet("{id}")]
    public Task<ProductModel> GetAsync(Guid id)
    {
        return Task.FromResult(InitDatas.First(i => i.Id == id));
    }
}
